package com.aaa.vegetablemarketexpress.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @FileName: WebMvcConfig
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/10 10:22
 * @Version: 1.0.0
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/*    // 从配置文件注入本地存储根目录和访问前缀
    @Value("${file.upload.base-path}")
    private String baseUploadPath;

    @Value("${file.access.prefix}")
    private String fileAccessPrefix;

    @Value("${file.upload.business-licence-path}")
    private String businessLicencePath;

    @Value("${file.upload.avatar-path}")
    private String avatarPath;

    @Value("${file.upload.id-photo-path}")
    private String idPhotoPath;

    @Value("${file.upload.goods-image-path}")
    private String goodsImagePath;
    @Value("${file.upload.image-url-path}")// 映射商品(product)图片访问路径
    private String imageUrlPath;

    @Value("${file.upload.shopLogo-path}")
    private String shopLogoPath;
    *//**
     * 配置静态资源映射：将 "/uploads/**" 路径映射到本地目录
     *//*
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 格式：addResourceHandler(前端访问路径) -> addResourceLocations(本地目录路径)
        // 注意：本地路径必须以 "file:" 开头（表示文件系统路径）
        registry.addResourceHandler(fileAccessPrefix + "**")
                .addResourceLocations("file:" + baseUploadPath);
        // 映射营业执照文件访问路径
        registry.addResourceHandler("/uploads/business-licence/**")
                .addResourceLocations("file:" + businessLicencePath);
        // 映射身份证文件访问路径
        registry.addResourceHandler("/uploads/id-photo/**")
                .addResourceLocations("file:" + idPhotoPath);
        // 映射头像文件访问路径
        registry.addResourceHandler("/uploads/avatar/**")
                .addResourceLocations("file:" + avatarPath);
        // 映射商品图片访问路径
        registry.addResourceHandler("/uploads/goodsImage/**")
                .addResourceLocations("file:" + goodsImagePath);
        // 店铺logo
        registry.addResourceHandler("/uploads/shopLogo/**")
                .addResourceLocations("file:" + shopLogoPath);
        // 保留Spring Boot默认的静态资源映射（如resources/static目录）
        registry.addResourceHandler("/uploads/imageUrl/**")
                .addResourceLocations("file:"+imageUrlPath);
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    *//**
     * 初始化上传目录
     *//*
    @PostConstruct
    public void init() {
        // 获取项目根路径
        String projectPath = System.getProperty("user.dir");

        // 创建必要的上传目录
        String[] dirs = {
                projectPath + "/uploads/business-licence/",
                projectPath + "/uploads/avatar/",
                projectPath + "/uploads/id-photo/",
                projectPath + "/uploads/goodsImage/",
                projectPath + "/uploads/imageUrl/",

                projectPath + "/uploads/goodsImage/",
                projectPath + "/uploads/shopLogo/"
        };

        for (String dir : dirs) {
            try {
                Path path = Paths.get(dir);
                if (!Files.exists(path)) {
                    Files.createDirectories(path);
                    System.out.println("成功创建目录: " + dir);
                }
            } catch (Exception e) {
                System.err.println("创建目录失败: " + dir + ", 错误: " + e.getMessage());
            }
        }
    }*/
}
